<?php
$page_table = DB::$tables['page'];
$access_type_table = DB::$tables['access_type'];

$form_name="page_form"."_";
if( isset($_REQUEST[ $form_name."posted"]) ){
	
	$checkMandatory = DB::checkMandatory( $page_table, $_REQUEST ); //myPrint($checkMandatory,'$checkMandatory'); myPrint($_REQUEST,'$_REQUEST');
	if(count($checkMandatory)>0){
		Page::message( 'error', null, Lang::get('REQUIRED_FIELDS') );
		foreach($checkMandatory as $name=>$value){
			Page::message( 'error', $name, $value );
			Form::error( $form_name, $name, Lang::get('REQUIRED_FIELDS') );
		}		
	}
		
	if( isset($_REQUEST['page_access']) && count($_REQUEST['page_access'])>0) 
		$_REQUEST['page_access'] = serialize( $_REQUEST['page_access'] );
	else 	$_REQUEST['page_access']='';
	
	if(count(Page::$messages['error'])<1){
		try{
			$requestArr = DB::createRequestArr( array($page_table) ); 
			
			$sql='SELECT `page_path` FROM '.$page_table.' WHERE page_path="'.$_REQUEST['page_path'].'"';
			$res = exec_sql2list( $sql ); //myPrint($res,'$res'); 
			
			if(count($res)>0){
				$operation = Lang::get('TO_EDIT').' '.$_REQUEST['page_path'];
				DB::updateTableArrayWhere( $page_table, $requestArr[ $page_table ], 'page_path="'.$_REQUEST['page_path'].'"' );			
			}else{
				$operation = Lang::get('TO_ADD').' '.$_REQUEST['page_path'];
				DB::insertTableArray( $page_table, $requestArr[ $page_table ] );
			}
			
			Page::message( 'success', null, Lang::get('OPERATION_IS_EXECUTED').': '.$operation );
		}catch(Exception $e){
			Page::message( 'error', null, Lang::get('ERROR_OF_EXECUTION').': '.$operation );
		}	
	}


	foreach( $_REQUEST as $name=>$value) Form::value( $form_name, $name, $value);
	//myPrint( Form::$values, 'Form::$values'); myPrint( Form::$errors, 'Form::$errors');
}
//$_REQUEST['page_path']='/admin';//'/test'; //
if(!empty( $_REQUEST['page_path'] )){
	//global $page_table;
	/* имена полей таблицы */
	$page_path = 'page_path';
	/* ищем страницу по пути или псевдониму */
	$page_prop= exec_sql2array('SELECT * FROM `'.$page_table.
			'` WHERE `'.$page_path.'`="'.$_REQUEST['page_path'].'" ;');
			
	/* вычисляем свойства родительских разделОВ  */
	$prnt_page_prop = Page::getParentProp( $_REQUEST['page_path'] );	
	if( count($prnt_page_prop)>0 ){
		$page_prop = array_merge($page_prop, $prnt_page_prop[0]); $page_prop['$parent_path_prop']=$prnt_page_prop[0];
	}
	
	$page_prop=reset($page_prop); 
	$page_prop['page_access'] = unserialize( $page_prop['page_access']);
	
	Form::value( $form_name, 'page_path', $_REQUEST['page_path'] );
	foreach( $page_prop as $name=>$value ) Form::value( $form_name, $name, $value);
	
}

?>
<h3>Свойства страницы или раздела</h3>
<?php echo Page::outMessages();echo Form::outErrors( $form_name );?>
<form  method="post" name="<?php echo $form_name?>" action="">
<input type="hidden" name="<?php echo $form_name?>posted" value="1"/>
	<table class="form_table">
	<tbody>
<tr>
	<td width="250">Адрес страницы или раздела<b style="color:red">*</b>	</td>
	<td>
		<input type="text" name="page_path" id="page_path" value="<?php echo Form::value( $form_name, "page_path");?>" >
	</td>
</tr>
<tr>
	<td width="250">Псевдоним адреса	</td>
	<td>
		<input type="text" name="page_alias" id="page_alias" value="<?php echo Form::value( $form_name, "page_alias");?>" >
	</td>
</tr>
<tr>
	<td width="250">Доступ для групп	</td>
	<td>
		<select multiple="multiple" name="page_access[]" id="page_access" >
		<?php
			$access_types= exec_sql2array('SELECT * FROM  `'.$access_type_table.'`');
			foreach($access_types as $arr) $selectOptions[$arr['access_type_id']] = $arr['access_type_name'];
			$sel = isset( $page_prop, $page_prop['page_access'] ) ? $page_prop['page_access'] : null;
			echo Form::createSelectOptions( $selectOptions, $sel );
		?>
		</select>
	</td>
</tr>
<tr>
	<td width="250">
		Шаблон дизайна
<?php 
	$onclick="onclick=\"var hr=this.getAttribute('data-href'); if( hr && hr!='NONE' )window.open( hr+'/'+'preview.png', 'admin_popur_image', 'width=600,height=600, menubar=no,location=yes,resizable=yes,scrollbars=yes,status=no'); return false;\"";
	
	if(!empty($page_prop['parent_template']) && $page_prop['parent_template']!='NONE')
	echo '<a '.$onclick.' href="#" data-href="'.$page_prop['parent_template'].'">родительский шаблон...</a>';
?>
	</td>
	<td>
		<select name="page_template" id="page_template" value="<?php echo Form::value( $form_name, "page_template");?>" onchange="var el=document.getElementById('page_template_view');el.setAttribute('data-href', this.value);">
		<?php
			$files_array = files_array( _DOCUMENT_ROOT.TMPL_DIR, false, false);
			$templates = array(''=>'наследовать', 'NONE'=>'не использовать');
			foreach( reset($files_array) as $path=>$arr){
				$key = str_replace( _DOCUMENT_ROOT, '', $path);
				$templates[$key] = basename( $path );
			}
			$sel = isset( $page_prop, $page_prop['page_template'] ) ? $page_prop['page_template'] : null;
			echo Form::createSelectOptions( $templates, $sel ); 
		?>
		</select>
		<?php
			$onclickIm="onclick=\"var hr=this.getAttribute('data-href'); if( hr && hr!='NONE' )window.open( '../show_image.php?src='+hr+'/'+'preview.png', 'admin_popur_image', 'width=600,height=600, menubar=no,location=yes,resizable=yes,scrollbars=yes,status=no'); return false;\"";
			echo '<a class="view_but" id="page_template_view" href="#" '.$onclickIm.' data-href="'.Form::value( $form_name, "page_template").'" ></a><br/>';
		?>
	</td>
</tr>
<tr>
	<td width="250">Заголовок страницы	</td>
	<td>
		<input type="text" name="page_title" id="page_title" value="<?php echo Form::value( $form_name, "page_title");?>" >
	</td>
</tr>
<tr>
	<td width="250">Описание meta_description	</td>
	<td>
		<input type="text" name="page_meta_desc" id="page_meta_desc" value="<?php echo Form::value( $form_name, "page_meta_desc");?>" >
	</td>
</tr>
<tr>
	<td width="250">Ключевые слова meta_keywords	</td>
	<td>
		<input type="text" name="page_meta_key" id="page_meta_key" value="<?php echo Form::value( $form_name, "page_meta_key");?>" >
	</td>
</tr>
<tr>
	<td width="250">Добавление кода в head	</td>
	<td>
		<textarea name="page_add_head" id="page_add_head" ><?php echo Form::value( $form_name, "page_add_head");?></textarea>
	</td>
</tr>
<tr>
	<td width="250">Содержимое html	</td>
	<td>
		<textarea name="page_html" id="page_html" ><?php echo Form::value( $form_name, "page_html");?></textarea>
	</td>
</tr>
<tr>
	<td width="250">Загружать контент из базы( из поля html или искать файл)	</td>
	<td>
		<select name="page_load_from_db" id="page_load_from_db" value="<?php echo Form::value( $form_name, "page_load_from_db");?>" >
		<?php
		echo Form::createSelectOptions( 
				array('0'=>Lang::get('NO'), '1'=>Lang::get('YES'),), 
				( isset( $page_prop, $page_prop['page_load_from_db'] ) ? $page_prop['page_load_from_db'] : null)
			);
		?>
		</select>
	</td>
</tr>
<tr>
	<td width="250">Служебное описание страницы</td>
	<td>
		<input type="text" name="page_note" id="page_note" value="<?php echo Form::value( $form_name, "page_note");?>" >
	</td>
</tr>
	</tbody>	
	</table>
	<input type="submit" name="submit" value="Save"/>
</form>
<?php
	if(isset($page_prop)) myPrint( $page_prop, '$page_prop');
	myPrint($access_types,'$access_types');
	myPrint($selectOptions,'$selectOptions');
	myPrint($files_array,'$files_array');
?>